package com.a3dweijing.wetouch2.Activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.CheckCode;
import com.a3dweijing.wetouch2.bean.FlagMag;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.a3dweijing.wetouch2.tools.Str2MD5;
import com.google.gson.Gson;

/**
 * Created by Yxd on 2017/10/23.
 */

public class ForgetPasswordActivity extends BaseActivity {
    private Handler handler;
    private String phoneStr;
    private TextView phoneTxv;
    private EditText checkCodeEdt;
    private TextView timerTxv;
    private EditText passwordEdt;
    private TextView nextBtn;
    private String checkcodeStr="qwerty";

    private CountDownTimer timer;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgetpwd);
        phoneStr=getIntent().getStringExtra("phone_number");
        phoneTxv= (TextView) findViewById(R.id.forgetpassword_phonenum_txv);
        checkCodeEdt= (EditText) findViewById(R.id.forgetpassword_check_code_edt);
        timerTxv= (TextView) findViewById(R.id.forgetpassword_timer_txv);
        timerTxv.setEnabled(false);
        passwordEdt= (EditText) findViewById(R.id.forgetpassword_password_edt);
        nextBtn= (TextView) findViewById(R.id.forgetpassword_next_btn);
        phoneTxv.setText(phoneStr);
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String check = checkCodeEdt.getText().toString();
                if (check != null && check.length() == 6) {
                    if(Str2MD5.MD5(check).equals(checkcodeStr))
                    {
                        String pwd=passwordEdt.getText().toString();
                        if (pwd!=null && pwd.length()>=6 && pwd.length()<=18){
                            new FixPasswordThread(phoneStr,pwd).start();
                            startPopupwindow(getBaseContext(),"修改密码中...");
                        }else {
                            Toast.makeText(getBaseContext(),"请输入6-18位的密码", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(getBaseContext(),"验证不正确", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getBaseContext(),"验证不正确", Toast.LENGTH_SHORT).show();
                }
            }
        });
        timerTxv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timer.start();
                timerTxv.setEnabled(false);
                new getcheckcodeThread(phoneStr).start();
            }
        });
        timer= new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                timerTxv.setText((millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                timerTxv.setEnabled(true);
                timerTxv.setText("获取验证码");
            }
        };
        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 100:
                        endPopupwindow();
                        FlagMag flagMag=new Gson().fromJson(msg.obj.toString(),FlagMag.class);
                        if (flagMag.isFlag())
                        {
                            finish();
                            Toast.makeText(getBaseContext(),flagMag.getMsg(), Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(getBaseContext(),flagMag.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                        Log.e("--",msg.obj.toString());

                        break;
                    case 200:
                        CheckCode code=new Gson().fromJson(msg.obj.toString(),CheckCode.class);
                        if (code.isFlag())
                        {
                            checkcodeStr=code.getCode();
                        }
                        Log.e("--",msg.obj.toString());
                        break;
                    case 404:
                        endPopupwindow();
                        Toast.makeText(getBaseContext(),msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        break;
                }

            }
        };
        new getcheckcodeThread(phoneStr).start();
        timer.start();
    }

    private class FixPasswordThread extends Thread {
        String phone_number;
        String password;

        public FixPasswordThread(String phone_number, String password) {
            this.phone_number = phone_number;
            this.password = password;
        }
        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotFixPassword(phone_number, password);
            Log.e("--", resulet);
            if (resulet != null && resulet.length() > 7) {
                handler.sendMessage(handler.obtainMessage(100, resulet));
            }else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }
    }
    private class getcheckcodeThread extends Thread {
        String phone_number;

        public getcheckcodeThread(String phone_number) {
            this.phone_number = phone_number;
        }
        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotGetCheckCode(phone_number);
            Log.e("--", resulet);
            if (resulet != null && resulet.length() > 7) {
                handler.sendMessage(handler.obtainMessage(200, resulet));
            }else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }
    }
}
